﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using MySql.Data.MySqlClient;

namespace CodeRobot.DAL
{
    public class ModelHelper
    {
        /// <summary>
        /// 根据表名生成实体类
        /// </summary>
        /// <param name="strFilePath">保存路径</param>
        /// <param name="strProjectName">项目名</param>
        /// <param name="strTableName">表名</param>
        public static void CreateModelClass(string strFilePath, string strProjectName, string strTableName,string strFunctionList)
        {
            string strClassName = CodeRobot.Utility.StringHelper.GetClassName(strTableName);//类名
            strClassName = CodeRobot.Utility.StringHelper.GetTableNameUpper(strClassName);

            Directory.CreateDirectory(strFilePath);
            StreamWriter sw = new StreamWriter(strFilePath + "\\" + strClassName + "Model.cs", false, Encoding.GetEncoding("utf-8"));
            sw.WriteLine("using System;");
            sw.WriteLine("using System.Collections.Generic;");
            sw.WriteLine("using System.Text;");
            sw.WriteLine("");
            sw.WriteLine("namespace " + CodeRobot.Utility.StringHelper.GetTableNameUpper(strProjectName) + ".Model");
            sw.WriteLine("{");
            sw.WriteLine("");
            sw.WriteLine("/// <summary>");
            sw.WriteLine("/// 版权所有: Copyright © " + DateTime.Now.Year.ToString() + " Cloudin. 保留所有权利。");
            sw.WriteLine("/// 内容摘要: " + strClassName + "Model");
            sw.WriteLine("/// 完成日期：" + DateTime.Now.ToString("yyyy年M月d日"));
            sw.WriteLine("/// 版    本：V1.0 ");
            sw.WriteLine("/// 作    者：Adin.Lee");
            sw.WriteLine("/// </summary>");
            sw.WriteLine("public class " + strClassName + "Model");
            sw.WriteLine("{");
            sw.WriteLine(strFunctionList);
            sw.WriteLine("}");
            sw.WriteLine("}");
            sw.Close();

        }


        public static void CreateAndroidModelClass(string strFilePath, string strProjectName, string strTableName, string strPackage)
        {
            string strClassName = CodeRobot.Utility.StringHelper.GetClassName(strTableName);//类名
            strClassName = CodeRobot.Utility.StringHelper.GetTableNameUpper(strClassName);

            Directory.CreateDirectory(strFilePath);
            StreamWriter sw = new StreamWriter(strFilePath + "\\" + strClassName + "Model.java", false, Encoding.GetEncoding("utf-8"));
            sw.WriteLine("package "+ strPackage + ".model;");
            sw.WriteLine("\r\n");
            sw.WriteLine("import org.json.JSONObject;");
            sw.WriteLine("\r\n");
            sw.WriteLine("/**");
            sw.WriteLine(" * "+ strTableName);
            sw.WriteLine(" * Author: Adin Lee.");
            sw.WriteLine(" * Date: "+ DateTime.Now.ToString("yyyy年M月d日") + ".");
            sw.WriteLine(" * Copyright (c) 2016 Cloudin. All rights reserved.");
            sw.WriteLine(" */");
            sw.WriteLine("public class "+ strClassName + "Model {");
            sw.WriteLine(GetDefineParaList(strTableName) + "\r\n");
            sw.WriteLine("    public " + strClassName + "Model(JSONObject json) {");
            sw.WriteLine("\r\n");
            sw.WriteLine(GetJSONParaList(strTableName) + "\r\n");
            sw.WriteLine("    }");
            sw.WriteLine("}");
            sw.Close();

        }


        /// <summary>
        /// 获取定义参数列表
        /// </summary>
        /// <param name="strTableName">表名</param>
        /// <returns></returns>
        public static string GetDefineParaList(string strTableName)
        {
            string strReturnValue = "";

            try
            {
                int nNum = 0;
                MySqlConnection cn = new MySqlConnection(CodeRobot.DBSqlHelper.DBMySQLHelper.ConnectionMySQL());
                cn.Open();
                string strSql = "SELECT COLUMN_NAME,COLUMN_KEY,COLUMN_COMMENT,DATA_TYPE FROM `information_schema`.`COLUMNS` WHERE TABLE_NAME='" + strTableName + "'";
                MySqlCommand cmd = new MySqlCommand(strSql, cn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string strColumnName = dr["COLUMN_NAME"].ToString();//字段列名
                    string strColumnNameUpper = CodeRobot.Utility.StringHelper.GetTableNameUpper(strColumnName);//如：UserName
                    string strColumnKey = dr["COLUMN_KEY"].ToString().Trim();//是否是主键
                    string strColumnComment = dr["COLUMN_COMMENT"].ToString();//注释
                    strColumnComment = CodeRobot.Utility.StringHelper.GetColumnComment(strColumnComment);

                    string strDataType = dr["DATA_TYPE"].ToString();//数据类型
                    strDataType = CodeRobot.Utility.StringHelper.GetCSharpDBType(strDataType);

                    //获取数据类型
                    string strValue = "";
                    if (strDataType == "int")
                    {
                        strValue = "int";
                    }
                    else if (strDataType == "float")
                    {
                        strValue = "int";
                    }
                    else if (strDataType == "double")
                    {
                        strValue = "int";
                    }
                    else if (strDataType == "decimal")
                    {
                        strValue = "int";
                    }
                    else if (strDataType == "bool")
                    {
                        strValue = "int";
                    }
                    else if (strDataType == "DateTime")
                    {
                        strValue = "long";
                    }
                    else if (strDataType == "string")
                    {
                        strValue = "String";
                    }
                    else
                    {
                        strValue = "String";
                    }

                    string strColumn = "";
                    if(strValue == "int")
                    {
                        strColumn = "    public int "+ strColumnName + ";";
                    }
                    else if (strValue == "long")
                    {
                        strColumn = "    public int " + strColumnName + " = 0;";
                    }
                    else
                    {
                        strColumn = "    public String " + strColumnName + " = \"\";";
                    }

                    strReturnValue += strColumn + "\r\n";
                    nNum++;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                CodeRobot.Utility.LogHelper.Error(typeof(ModelHelper), ex, "获取定义参数列表", "GetDefineParaList");
            }

            return strReturnValue;
        }
        
        /// <summary>
        /// 获取读取服务端JSON数据代码列表
        /// </summary>
        /// <param name="strTableName">表名</param>
        /// <returns></returns>
        public static string GetJSONParaList(string strTableName)
        {
            string strReturnValue = "";

            try
            {
                int nNum = 0;
                MySqlConnection cn = new MySqlConnection(CodeRobot.DBSqlHelper.DBMySQLHelper.ConnectionMySQL());
                cn.Open();
                string strSql = "SELECT COLUMN_NAME,COLUMN_KEY,COLUMN_COMMENT,DATA_TYPE FROM `information_schema`.`COLUMNS` WHERE TABLE_NAME='" + strTableName + "'";
                MySqlCommand cmd = new MySqlCommand(strSql, cn);
                MySqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string strColumnName = dr["COLUMN_NAME"].ToString();//字段列名
                    string strColumnNameUpper = CodeRobot.Utility.StringHelper.GetTableNameUpper(strColumnName);//如：UserName
                    string strColumnKey = dr["COLUMN_KEY"].ToString().Trim();//是否是主键
                    string strColumnComment = dr["COLUMN_COMMENT"].ToString();//注释
                    strColumnComment = CodeRobot.Utility.StringHelper.GetColumnComment(strColumnComment);

                    string strDataType = dr["DATA_TYPE"].ToString();//数据类型
                    strDataType = CodeRobot.Utility.StringHelper.GetCSharpDBType(strDataType);

                    //获取数据类型
                    string strValue = "";
                    if (strDataType == "int")
                    {
                        strValue = "int";
                    }
                    else if (strDataType == "float")
                    {
                        strValue = "int";
                    }
                    else if (strDataType == "double")
                    {
                        strValue = "int";
                    }
                    else if (strDataType == "decimal")
                    {
                        strValue = "int";
                    }
                    else if (strDataType == "bool")
                    {
                        strValue = "int";
                    }
                    else if (strDataType == "DateTime")
                    {
                        strValue = "long";
                    }
                    else if (strDataType == "string")
                    {
                        strValue = "String";
                    }
                    else
                    {
                        strValue = "String";
                    }

                    string strColumn = "";
                    if (strValue == "int")
                    {
                        strColumn = "        " + strColumnName + " = json.optInt(\"" + strColumnName + "\");";
                    }
                    else if (strValue == "long")
                    {
                        strColumn = "        " + strColumnName + " = json.optLong(\"" + strColumnName + "\");";
                    }
                    else
                    {
                        strColumn = "        " + strColumnName + " = json.optString(\"" + strColumnName + "\");";
                    }

                    strReturnValue += strColumn + "\r\n";
                    nNum++;
                }
                dr.Dispose();
                cn.Close();
            }
            catch (Exception ex)
            {
                CodeRobot.Utility.LogHelper.Error(typeof(ModelHelper), ex, "获取读取服务端JSON数据代码列表", "GetJSONParaList");
            }

            return strReturnValue;
        }

    }
}
